package com.hippy.erya.modular.erya;

import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.hippy.erya.base.auth.context.LoginContextHolder;
import com.hippy.erya.base.pojo.page.LayuiPageFactory;
import com.hippy.erya.base.pojo.page.LayuiPageInfo;
import com.hippy.erya.entity.RawMaterialDO;
import com.hippy.erya.entity.RawMaterialDictionaryDO;
import com.hippy.erya.entity.RawMaterialLossDO;
import com.hippy.erya.impl.RawMaterialDictionaryServiceImpl;
import com.hippy.erya.impl.RawMaterialServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/erya/rawmaterial")
public class RawMaterialController {

    @Autowired
    private RawMaterialServiceImpl rawMaterialService;

    @Autowired
    private RawMaterialDictionaryServiceImpl dictionaryService;

    @RequestMapping(value = "/rawmaterial")
    public String rawmaterial() {
        return "/erya/rawmaterial/rawmaterial_setting.html";
    }

    @RequestMapping(value = "/rawmaterialAdd")
    public String rawmaterialAdd() {
        return "/erya/rawmaterial/rawmaterial_add.html";
    }

    @RequestMapping(value = "/rawmaterialEdit")
    public String rawmaterialEdit() {
        return "/erya/rawmaterial/rawmaterial_edit.html";
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public LayuiPageInfo queryByPage(@PathParam("page") Long page, @PathParam("limit") int limit,
                                     @PathParam("materialCode") String materialCode,
                                     @PathParam("buyDate") String buyDate,
                                     @PathParam("buyChannel") String buyChannel) {
        String actualMaterialCode = null;
        if (!StringUtils.isEmpty(materialCode)) {
            Map<String, String> map = dictionaryService.nameToCode(LoginContextHolder.getContext().getUserId().toString());
            actualMaterialCode = map.get(materialCode);
            if (StringUtils.isEmpty(actualMaterialCode)) {
                return LayuiPageFactory.createNonDataInfo();
            }
        }
        return LayuiPageFactory.createPageInfo(rawMaterialService.queryByPage(page, limit, actualMaterialCode, buyDate, buyChannel));
    }

    @RequestMapping("/deleteData/{id}")
    @ResponseBody
    public ResponseData deleteData(@PathVariable("id") int id) {
        RawMaterialDO rawMaterialDO = rawMaterialService.getById(id);
        rawMaterialDO.deleteData(LoginContextHolder.getContext().getUserId().toString());
        rawMaterialService.updateById(rawMaterialDO);
        return ResponseData.success();
    }

    @RequestMapping("/addData")
    @ResponseBody
    public ResponseData addData(RawMaterialDO rawMaterialDO) {
        // 重复添加校验
        if (!checkRawMaterialAddDO(rawMaterialDO)) {
            return ResponseData.error("该食材采购信息已存在！");
        }
        String user = String.valueOf(LoginContextHolder.getContext().getUserId());
        rawMaterialDO.setUserCode(user);
        rawMaterialDO.setCreatedBy(user);
        rawMaterialDO.setUpdatedBy(user);
        rawMaterialDO.setUnitPricePergram(rawMaterialDO.getUnitPrice() / 500);
        rawMaterialService.save(rawMaterialDO);
        return ResponseData.success();
    }

    @RequestMapping("/editData")
    @ResponseBody
    public ResponseData editData(RawMaterialDO rawMaterialDO) {
        String user = String.valueOf(LoginContextHolder.getContext().getUserId());
        rawMaterialDO.setUpdatedBy(user);
        rawMaterialDO.setUnitPricePergram(rawMaterialDO.getUnitPrice() / 500);
        rawMaterialService.updateById(rawMaterialDO);
        return ResponseData.success();
    }


    @RequestMapping("/rawmaterialDetail/{id}")
    @ResponseBody
    public ResponseData editData(@PathVariable("id") int id) {
        RawMaterialDO rawMaterialDO = rawMaterialService.getById(id);
        RawMaterialDictionaryDO rawMaterialDictionaryDO = rawMaterialService.queryNameByRawMaterialCode(rawMaterialDO.getMaterialCode());
        if (null != rawMaterialDictionaryDO) {
            rawMaterialDO.setMaterialName(rawMaterialDictionaryDO.getName());
        }
        return ResponseData.success(rawMaterialDO);
    }

    private boolean checkRawMaterialAddDO(RawMaterialDO rawMaterialDO) {
        List<RawMaterialDO> rawMaterialLossList = rawMaterialService.queryByRawMaterialSame(rawMaterialDO);
        if (!CollectionUtils.isEmpty(rawMaterialLossList)) {
            return false;
        }
        return true;
    }
}
